<?php
require_once("../../../config.inc.php");
$isReloadable = false;
require_once("../../../app/_inc/include.php");

$errMsg = "";
if(sxREQUEST("reqtype")=='save') {
	$errMsg .= profDataAreaUpd();
}
$sSQL = "SELECT description from profile " .
			" WHERE id_profile = ".sxREQUEST('id');
//dbg_($sSQL);
$query = $objSDAO->query($sSQL);
$rowPR = $objSDAO->fetch();
$sSQL = "SELECT da.description, da.data_area, pda.permission " .
			" FROM data_area da " .
			" LEFT OUTER JOIN data_area_permission pda " .
			" ON pda.data_area=da.data_area AND pda.id_profile = ".sxREQUEST('id') .
			" ORDER BY da.data_area";
//dbg_($sSQL);
$query = $objSDAO->query($sSQL);


//output HTML
$aOpz = array("tit2"=>_t("Abilitreqtype aree dati per profilo %s",sxHE($rowPR['description'])), "isReloadable"=>$isReloadable);
echo(headerHTML($aOpz));
?>
<script language="JavaScript" type="text/javascript">
	function submitPDA(reqtype){
		document.objFrm.reqtype.value = reqtype;
		document.objFrm.submit();
	}
</script>
<?php
echo(bodyHTML($aOpz));
//output comandi
$aCmd = array(	
					"registra"=>array("pag"=>"javascript:submitPDA('save');",
										"title"=>_t("registra le abilitazioni da DB"),
										"text"=>_t("registra")),
					"ricarica"=>array("pag"=>"javascript:submitPDA('reload');",
										"title"=>_t("ricarica le abilitazioni da DB"),
										"text"=>_t("ricarica")),
					"back"=>array("pag"=>$objUser->getHistory(0,false),
										"title"=>$objUser->getHistoryText(0),
										"text"=>$objUser->getHistoryText(0))
					);
echo(commandsHTML($aCmd));	
//output dei dati
echo("<table class=\"tabl01\" width=\"100%\" cellpadding=\"3\" cellspacing=\"2\">\n");
echo("<form name=\"objFrm\" id=\"objFrm\" action=\"".$_SERVER['PHP_SELF']."\" method=\"POST\">\n");
echo("<input type=\"hidden\" name=\"reqtype\" id=\"reqtype\" value=\"reload\">\n");
echo("<input type=\"hidden\" name=\"id\" id=\"id\" value=\"".sxREQUEST('id')."\">\n");
echo("<input type=\"hidden\" name=\"xsn\" id=\"xsn\" value=\"".XSN."\">\n");
echo("<tr>" .
		"<td class=\"titl01\" width=\"100%\" colspan='5'>"._t("Profilo").': '.sxHE($rowPR['description'])."</td>" .
	"</tr>\n");
echo("<tr>" .
		"<td class=\"tabh01\" width=\"15%\" align='center'>"._t("Non abilitato")."</td>" .
		"<td class=\"tabh01\" width=\"15%\" align='center'>"._t("Solo visualizza")."</td>" .
		"<td class=\"tabh01\" width=\"15%\" align='center'>"._t("Visualizza e modifica")."</td>" .
		"<td class=\"tabh01\" width=\"55%\">"._t("Area dati")."</td>" .
	"</tr>\n");
$nn=0;
while($row = $objSDAO->fetch()){
	$chkProf = "";
	if(trim($row["data_area"]) != "" && trim($row["permission"]) == "N"){
		$chkProf = "CHECKED";
	}
	$chkWrite = "";
	if(trim($row["permission"]) != "" && trim($row["permission"]) == "M"){
		$chkWrite = "CHECKED";
	}
	$chkRead = "";
	if($chkProf == "" && $chkWrite == ""){
		$chkRead = "CHECKED";
	}
	$nn++;
	echo("<tr>" .
			"<td class=\"text01\" align='center'>" .
				"<input class=\"text01\" type=\"radio\" name=\"perm_$nn\" value=\"N\" $chkProf></td>" .
			"<td class=\"text01\" align='center'>" .
				"<input class=\"text01\" type=\"radio\" name=\"perm_$nn\" value=\"R\" $chkRead></td>" .
			"<td class=\"text01\" align='center'>" .
				"<input class=\"text01\" type=\"radio\" name=\"perm_$nn\" value=\"M\" $chkWrite></td>" .
				"<input type=\"hidden\" name=\"da_$nn\" id=\"da_$nn\" value=\"".$row["data_area"]."\">" .
			"<td class=\"text01\">".$row["data_area"]." - ".$row["description"]."</td>" .
		"</tr>\n");
}
echo("</form>");
echo("</table>\n");
echo(commandsHTML($aCmd));
echo(footerHTML());
$objSDAO->close();

function profDataAreaUpd() {
	global $objSDAO;
	$sSQL = "DELETE FROM data_area_permission " .						
				"WHERE id_profile=".sxREQUEST('id');
	$objSDAO->exec($sSQL);
	foreach($_REQUEST as $key=>$val){
		if(substr($key,0,3) != "da_"){continue;}	
		$nn = substr($key,3);				
		$permission = (sxREQ("perm_$nn"));
		if($permission != "" && $permission != "R"){
			$sSQL = "INSERT INTO data_area_permission " .
					"(data_area, id_profile, permission)" .
					"VALUES" .
					"('".$val."',".sxREQUEST('id').",'$permission')";
			$objSDAO->exec($sSQL);
		}
	}
	//ricarico le permission dell'utente
	$_SESSION[USEROBJ]->reLoad();
	return '';
}

?>
